package class3;

/**
 * 双链表的反转
 */
public class Code02_ReverseDoubleList {
    public static void main(String[] args) {

        DoubleNode doubleNode1 = new DoubleNode(1);
        DoubleNode doubleNode2 = new DoubleNode(2);
        DoubleNode doubleNode3 = new DoubleNode(3);
        doubleNode1.next = doubleNode2;
        doubleNode2.next = doubleNode3;
        doubleNode2.last = doubleNode1;
        doubleNode3.last = doubleNode2;

        doubleNode1 = reverseDoubeList(doubleNode1);
        while (doubleNode1 != null) {
            System.out.println(doubleNode1.d);
            doubleNode1 = doubleNode1.next;
        }
    }

    private static DoubleNode reverseDoubeList(DoubleNode head) {
        DoubleNode pre = null;
        DoubleNode next = null;
        while (head != null) {
            next = head.next;
            head.next = pre;
            head.last = next; // 双向指针
            pre = head;
            head = next;

        }
        return pre;
    }

    public static class DoubleNode {
        int d;
        DoubleNode next;
        DoubleNode last;

        public DoubleNode(int d) {
            this.d = d;
        }
    }
}


